<?php
class Ab_File_Manager extends Ab_Controller{ 

    function index(){    	
        $params = $this->router->getParams();        
        
        $msg=array('type'=>'','content'=>'');   
        
        if(isset($_SESSION['ab_file_manager']['msg'])){
        	$msg=$_SESSION['ab_file_manager']['msg'];
        	unset($_SESSION['ab_file_manager']['msg']);
        }     
       
        $path=$default_path=ROOT_DIR.'/files/testFolder';
        if(isset($params['path'])){
        	$path=ROOT_DIR.'/'.base64_decode($params['path']);
        	
        }
       
       	$view = new Ab_View();
    	
    	try{ 
		$directory=new Ab_Directory($path);
        }
        //use default path+display error message
        catch(NotDirectoryException $e){
        	$path=$default_path;
        	$directory=new Ab_Directory($path);
        	$msg['type']='error';
        	$msg['content']=$e->getMessage();
        }
        
        $_SESSION['ab_file_manager']['current_path']=$path;
        
        $places=$directory->getDirChildren();
	$files=$directory->getFileChildren();
	
	$upper_dirs=$directory->getUpperDirs();
	
	$parent_path=null;
	if(is_array($upper_dirs) && !empty($upper_dirs)){
		if(isset($upper_dirs[count($upper_dirs)-2])){		
			$parent_path=$upper_dirs[count($upper_dirs)-2]['dir_path'];
		}		
	}
	
	//var_dump($parent_path);
	
	$body_content_vals=array('places'=>$places,'files'=>$files,'msg'=>$msg,'upper_dirs'=>$upper_dirs,'parent_path'=>$parent_path);
	        
        //set content
        $result_body_content = $view->render('home.php',$body_content_vals);                
	$result_layout = $view->render('frontend_default_layout.php',array('content'=>$result_body_content));
        
        $this->router->buildLayout($result_layout);
    }
    
    function createFolder(){
    	$msg['type']='error';
    	$msg['content']='Invalid parameters';
    	
    	
    	if(isset($_POST['dirname']) && trim($_POST['dirname'])!=''){
    		$directory=new Ab_Directory($_SESSION['ab_file_manager']['current_path']);
    		if($directory->addChildDir($_POST['dirname'])){
    			$msg['type']='ok';
    			$msg['content']='The folder was created';
    		}
    		else{
    			$msg['type']='error';
    			$msg['content']='Cannot create folder '.$_POST['dirname'].' in '.$_SESSION['ab_file_manager']['current_path'];
    		}
    	}
    	
    	$_SESSION['ab_file_manager']['msg']=$msg;
    	
    	//redirect
    	header("Location: ".SITE_URL.'index.php/file_manager/index/path:'.base64_encode(ltrim($_SESSION['ab_file_manager']['current_path'],ROOT_DIR)));
    	exit;	
    }
    
    
    function uploadFile(){    	
    	$file=new Ab_File();
    	$msg=array('type'=>'','content'=>'');
    	
    	if(isset($_FILES['file'])){
    		if(!$file->upload($_FILES['file'],$_SESSION['ab_file_manager']['current_path'])){
    			$msg['type']='error';
    			$msg['content']=implode($file->getErrors(),'<br />');
    		}
    		else{
    			if($_POST['rename_to']!=''){
    				$extension=$file->getExtension();
	    			
	    			$new_path=$_SESSION['ab_file_manager']['current_path'].'/'.$_POST['rename_to'].$extension;
	    			$file->rename($new_path);
    			}
    			
    			$msg['type']='ok';
    			$msg['content']='The file was uploaded to '.$_SESSION['ab_file_manager']['current_path'];
    		}
    		
    		
    	}
    	
    	$_SESSION['ab_file_manager']['msg']=$msg;
    	
    	//redirect
    	header("Location: ".SITE_URL.'index.php/file_manager/index/path:'.base64_encode(ltrim($_SESSION['ab_file_manager']['current_path'],ROOT_DIR)));
    	exit;	
    }
    
    function removeFiles(){
    	$msg['type']='error';
    	$msg['content']='Invalid parameters';
    	
    	if(isset($_POST['remove_file']) && !empty($_POST['remove_file'])){
    		$directory=new Ab_Directory($_SESSION['ab_file_manager']['current_path']);
    		if($directory->removeFile($_POST['remove_file'])){
    			$msg['type']='ok';
    			$msg['content']='The file(s) have been removed';
    		}
    		else{
    			$msg['type']='error';
    			$msg['content']='Cannot remove file(s)';
    		}
    	}
    	
    	$_SESSION['ab_file_manager']['msg']=$msg;
    	
    	//redirect
    	header("Location: ".SITE_URL.'index.php/file_manager/index/path:'.base64_encode(ltrim($_SESSION['ab_file_manager']['current_path'],ROOT_DIR)));
    	exit;
    }
    
    
    function removeDirs(){
    	$msg['type']='error';
    	$msg['content']='Invalid parameters';
    	    	
    	if(isset($_POST['remove_dir']) && !empty($_POST['remove_dir'])){
    		$directory=new Ab_Directory($_SESSION['ab_file_manager']['current_path']);
    		
    		if($directory->removeDir($_POST['remove_dir'])){
    			$msg['type']='ok';
    			$msg['content']='The folder(s) have been removed';
    		}
    		else{
    			$msg['type']='error';
    			$msg['content']='Cannot remove folder(s)';
    		}
    	}
    	
    	$_SESSION['ab_file_manager']['msg']=$msg;
    	
    	//redirect
    	header("Location: ".SITE_URL.'index.php/file_manager/index/path:'.base64_encode(ltrim($_SESSION['ab_file_manager']['current_path'],ROOT_DIR)));
    	exit;
    }
    
    
    function getPreview(){
	$params = $this->router->getParams();        

	$msg['type']='error';
	$msg['content']='Invalid parameters'; 

	if(isset($params['preview'])){
		$preview_file=$params['preview'];
		
		try{
			$file=new Ab_File($_SESSION['ab_file_manager']['current_path'].'/'.$params['preview']);
			
			$file_type_obj=$file->getPathType();
			
			$preview_path=$file_type_obj->getPreview();
			
			$msg['type']='ok';
			$msg['content']=ltrim($preview_path,ROOT_DIR);  
		}
		catch(Exception $e){
			$msg['type']='error';
			$msg['content']='Error occured: '.$e->getMessage();
		}
	}
	
	echo json_encode($msg);         
    }
    
   
   
}
?>
